using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using AutoMapper;
using Core.SimpleTemp.Repositories.Internal;
using Microsoft.EntityFrameworkCore;
using Core.SimpleTemp.Api.Mobile;
using Core.SimpleTemp.Entitys;
using Microsoft.AspNetCore.Http;
using Core.SimpleTemp.Application.ServiceApp.SysApp.FileApp;
using Core.Toolkit.Message;
using Core.SimpleTemp.Application.common;
using Microsoft.Extensions.Options;

namespace Core.SimpleTemp.Mvc.Api.sys
{
    /// <summary>
    /// //法律法规_标准库
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
                   // [Authorize]//验证是否登录
    public class FL_StandardInformationWebController : CoreApiController
    {
        private readonly FL_StandardInformationService _service;
        private readonly FL_StandardInformationHisService _Hisservice; //法律法规历史表
        private readonly CoreDBContext _coreDBContext;
        CoreDBContext _dbContext;
        private readonly IMessage _message;
        private readonly WrkerRelationsService _wrkerRelationsService;
        private readonly BusinessOptions _businessOptions;

        /// <summary>
        /// FL_StandardInformation
        ///</summary>
        public FL_StandardInformationWebController(FL_StandardInformationService service, FL_StandardInformationHisService Hisservice, IMessage message, WrkerRelationsService wrkerRelationsService, CoreDBContext coreDBContext, CoreDBContext dbContext, IOptionsMonitor<BusinessOptions> businessOptions)
        {
            _service = service;
            _Hisservice = Hisservice;
            _coreDBContext = coreDBContext;
            _dbContext = dbContext;
            _message = message;
            _wrkerRelationsService = wrkerRelationsService;
            _businessOptions = businessOptions.CurrentValue;
        }


        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<FL_StandardInformationDto>
                pagingQueryModel)
        {
            //IPageModel<FL_StandardInformationDto> result = await _service.GetList(pagingQueryModel);
            //PageModelObj retObj = new PageModelObj();
            //retObj.PageData = result.PageData?.Select(a => new { a.Id, a.LawRegulationsName }).ToList();
            //retObj.RowCount = result.RowCount;
            //return this.JsonSuccess(retObj);

            //工厂
            //var FactoryName = _coreDBContext.SysRole.Where(x => x.UpdateUser == id).FirstOrDefault().CreateFactory;
           // pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;

            IPageModel<FL_StandardInformationDto> result = null;
            result = await _service.GetList(pagingQueryModel);

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();
                dto.FL_StandardInformationDept2 = dto.FL_StandardInformations.Where(x => x.DeptType == "2").ToList();
                dto.FL_StandardInformationDept3 = dto.FL_StandardInformations.Where(x => x.DeptType == "3").ToList();


                var str = "";
                dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark03 = str;

                str = "";
                dto.FL_StandardInformationDept2.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark04 = str;

                str = "";
                dto.FL_StandardInformationDept3.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark05 = str;

                if (dto.State == 0.ToString())
                {
                    dto.StateName = "未审批";
                }
                if (dto.State == 1.ToString())
                {
                    dto.StateName = "已审批";
                }
                if (dto.FL_StandardInformationReview.Where(x => x.CAM_Type == "正常").ToList().Count > 0)
                {
                    var LastDtoReview = dto.FL_StandardInformationReview.Where(x=>x.CAM_Type=="正常").ToList().OrderBy(x => x.CreateTime).LastOrDefault();
                    dto.Mark06 = LastDtoReview.FitSituation;//符合情况
                    dto.Mark07 = LastDtoReview.FitSituationOrNotDescribe;//不符合情况

                    if (LastDtoReview.CAM_ReviewPerson != null)
                    {
                        dto.Mark08 = _coreDBContext.SysUser.Where(x => x.Id == LastDtoReview.CAM_ReviewPerson).FirstOrDefault().Name; //审批人
                    }
                    if (LastDtoReview.CAM_ReviewDate != null)
                    {
                        dto.Mark09 = Convert.ToDateTime(LastDtoReview.CAM_ReviewDate).ToString("yyyy-MM-dd ");//
                    }
                    if (LastDtoReview.Remarks != null)
                    {
                        dto.Mark10 = LastDtoReview.Remarks;//备注
                    }
                }
            });
           
            return this.JsonSuccess(result);
        }



        /// <summary>
        /// 法律法规历史清单----查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetHisList")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetHisList(PagingModel<FL_StandardInformationHisDto>
                pagingQueryModel)
        {
            //IPageModel<FL_StandardInformationDto> result = await _service.GetList(pagingQueryModel);
            //PageModelObj retObj = new PageModelObj();
            //retObj.PageData = result.PageData?.Select(a => new { a.Id, a.LawRegulationsName }).ToList();
            //retObj.RowCount = result.RowCount;
            //return this.JsonSuccess(retObj);

           // pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            IPageModel<FL_StandardInformationHisDto> result = null;
            result = await _Hisservice.GetList(pagingQueryModel);

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();
                dto.FL_StandardInformationDept2 = dto.FL_StandardInformations.Where(x => x.DeptType == "2").ToList();
                dto.FL_StandardInformationDept3 = dto.FL_StandardInformations.Where(x => x.DeptType == "3").ToList();
            });

            result.PageData.ForEach(dto =>
            {
                var str = "";
                dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark03 = str;
            });

            result.PageData.ForEach(dto =>
            {
                var str = "";
                dto.FL_StandardInformationDept2.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark04 = str;
            });

            result.PageData.ForEach(dto =>
            {
                var str = "";
                dto.FL_StandardInformationDept3.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark05 = str;
            });

            result.PageData.ForEach(dto =>
            {
                if (dto.State == 0.ToString())
                {
                    dto.StateName = "未审批";
                }
                if (dto.State == 1.ToString())
                {
                    dto.StateName = "已审批";
                }
            });
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 获得各职能部门具体识别清单
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetClassList")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetClassList(PagingModel<FL_StandardInformationDto>
                pagingQueryModel)
        {
            //工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            IPageModel<FL_StandardInformationDto> result = null;
            result = await _service.GetList(pagingQueryModel);

            List<FL_StandardInformationDto> List = new List<FL_StandardInformationDto>();
            var DeptEntity = _coreDBContext.SysDepartment;
            
            var entities = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法律" && x.IsDeleted==false).FirstOrDefault().Id).ToList();

            var entities_FG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法规" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_GJBM = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规章" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_GJBMGF = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规范" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_DFXFG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "地方性法规及要求" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_BZHGF = _coreDBContext.FL_StandardInformation
          .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "标准和规范" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_SJGSZD = _coreDBContext.FL_StandardInformation
         .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "上级公司制度及要求" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            for (int i = 0; i < DeptEntity.ToList().Count; i++)
            {
                var NewModel = new FL_StandardInformationDto();
                NewModel.Mark01 = DeptEntity.ToList()[i].Name;
                //国家法律
                int count = 0;
                int SumCount = 0;
                entities.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1"|| c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark02 = count.ToString();
                //国家法规
                 count = 0;
                entities_FG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark03 = count.ToString();

                //国家部门规章
                count = 0;
                entities_GJBM.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark04 = count.ToString();

                //国家部门规范
                count = 0;
                entities_GJBMGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark05 = count.ToString();
               
                //地方性法规及要求
                count = 0;
                entities_DFXFG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark06 = count.ToString();
                //标准和规范
                count = 0;
                entities_BZHGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark07 = count.ToString();

                  //标准和规范
                count = 0;
                entities_SJGSZD.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark08 = count.ToString();
                NewModel.Mark09 = SumCount.ToString();
                List.Add(NewModel);
            }
            return this.JsonSuccess(List.ToArray());
        }



        /// <summary>
        /// 获得法律法规和其他要求及更新清单
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetClassList2")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetClassList2(PagingModel<FL_StandardInformationDto>
                pagingQueryModel)
        {
            //工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            IPageModel<FL_StandardInformationDto> result = null;
            result = await _service.GetList(pagingQueryModel);

            List<FL_StandardInformationDto> List = new List<FL_StandardInformationDto>();
            var DeptEntity = _coreDBContext.SysDepartment;

            var entities = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法律" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_FG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法规" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_GJBM = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规章" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_GJBMGF = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规范" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_DFXFG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "地方性法规及要求" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_BZHGF = _coreDBContext.FL_StandardInformation
          .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "标准和规范" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var entities_SJGSZD = _coreDBContext.FL_StandardInformation
         .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "上级公司制度及要求" && x.IsDeleted == false).FirstOrDefault().Id).ToList();

            var NewModel = new FL_StandardInformationDto();
            int SumCount_GJFL = 0;
            int SumCount_GJFG = 0;
            int SumCount_GJBMGZ = 0;
            int SumCount_GJBMGF = 0;
            int SumCount_DFXFGJYQ = 0;
            int SumCount_BZHGF = 0;
            int SumCount_GSZDJYQ = 0;

            for (int i = 0; i < DeptEntity.ToList().Count; i++)
            {
                NewModel.Mark01 = DeptEntity.ToList()[i].Name;
                //国家法律
                int count = 0;
             
                entities.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJFL += count;
                NewModel.Mark02 = count.ToString();
                //国家法规
                count = 0;
                entities_FG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJFG += count;
                NewModel.Mark03 = count.ToString();

                //国家部门规章
                count = 0;
                entities_GJBM.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJBMGZ += count;
                NewModel.Mark04 = count.ToString();

                //国家部门规范
                count = 0;
                entities_GJBMGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJBMGF += count;
                NewModel.Mark05 = count.ToString();

                //地方性法规及要求
                count = 0;
                entities_DFXFG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_DFXFGJYQ += count;
                NewModel.Mark06 = count.ToString();
                //标准和规范
                count = 0;
                entities_BZHGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_BZHGF += count;
                NewModel.Mark07 = count.ToString();

                //上级公司制度及要求
                count = 0;
                entities_SJGSZD.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].Id;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GSZDJYQ += count;
                NewModel.Mark08 = count.ToString();
              //  NewModel.Mark09 = SumCount.ToString();
            }
            NewModel.SumCount_GJFL = SumCount_GJFL.ToString();
            NewModel.SumCount_GJFG = SumCount_GJFG.ToString();
            NewModel.SumCount_GJBMGZ = SumCount_GJBMGZ.ToString();
            NewModel.SumCount_GJBMGF = SumCount_GJBMGF.ToString();
            NewModel.SumCount_DFXFGJYQ = SumCount_DFXFGJYQ.ToString();
            NewModel.SumCount_BZHGF = SumCount_BZHGF.ToString();
            NewModel.SumCount_GSZDJYQ = SumCount_GSZDJYQ.ToString();
            int SumTotal = SumCount_GJFL + SumCount_GJFG + 
                SumCount_GJBMGZ + SumCount_GJBMGF + 
                SumCount_DFXFGJYQ + SumCount_BZHGF + 
                SumCount_GSZDJYQ;
            NewModel.Mark10 = SumTotal.ToString();
            List.Add(NewModel);
            return this.JsonSuccess(List.ToArray());
        }








        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var entity = await _service.GetAsync(id);
            if (entity == null) return this.JsonFaild("未找到对应数据:" + id);

            //法律法规_标准库相关部门
            var standardInformationDeptDto = Mapper.Map<List<FL_StandardInformationDeptDto>>(entity.FL_StandardInformations);
            var FitSituation = "";
            var FitSituationOrNotDescribe = "";
            var Remarks = "";
           if (_coreDBContext.FL_StandardInformationReview.Where(x => x.StandardInformationID == id).ToList().Count>0)
            {
                //FitSituation = _coreDBContext.FL_StandardInformationReview.Where(x => x.StandardInformationID == id).LastOrDefault().FitSituation;
                //FitSituationOrNotDescribe = _coreDBContext.FL_StandardInformationReview.Where(x => x.StandardInformationID == id).LastOrDefault().FitSituationOrNotDescribe;
                //Remarks = _coreDBContext.FL_StandardInformationReview.Where(x => x.StandardInformationID == id).LastOrDefault().Remarks;
                FitSituation = entity.FL_StandardInformationReview.Where(x=>x.FitSituation !=null).LastOrDefault()?.FitSituation;
                FitSituationOrNotDescribe= entity.FL_StandardInformationReview.Where(x => x.FitSituationOrNotDescribe != null).LastOrDefault()?.FitSituationOrNotDescribe;
            }
            var standardInformationDept = standardInformationDeptDto?.Select(a =>
            new
            {
                departmentName = a.Mark01,//部门名称
                departmentID = a.Class,
                a.DeptType,
                a.DeptTypeText,//
                FitSituation,
                FitSituationOrNotDescribe,
                Remarks,
            });

            var DepId = standardInformationDept?.Where(a => a.DeptType == "1").FirstOrDefault().departmentID; //归口部门ID

            var  DepIdList= standardInformationDept?.Where(a => a.DeptType == "2" || a.DeptType == "3").Select(x=>x.departmentID).ToList(); //实施部门 了解部门 LisT

            //   var zxbmList = new List<SysDepartment>();
            var zxbmList1 = _coreDBContext.SysDepartment.Where(x => DepIdList.Contains(x.Id)).ToList();

           var zxbmList= zxbmList1.Select(a => new { a.Name });


            //  附件
            var attachmentList = await _coreDBContext.SysFile.Where(a => a.B_ID == id).Select(a => "/api/File/DownLoad?id=" + a.Id).ToListAsync();
            var CAM_Compliance="符合";
            var CAM_ComplianceStatement = "说明";
            var CAM_Mark = "备注";

            if (entity.FL_StandardInformationReview.LastOrDefault() != null)
            {
                if (entity.FL_StandardInformationReview.LastOrDefault().FitSituation != "")
                {
                    CAM_Compliance = "符合";
                    CAM_ComplianceStatement = entity.FL_StandardInformationReview.LastOrDefault().FitSituation;
                }
                else
                {
                    CAM_Compliance = "不符合";
                    CAM_ComplianceStatement = entity.FL_StandardInformationReview.LastOrDefault().FitSituationOrNotDescribe;

                }
                CAM_Mark = entity.FL_StandardInformationReview.LastOrDefault().Remarks;
            }

        
            var retModel = new
            {
                FileLeveName = entity.FL_FileLevel.FileLeve,
                entity.Id,//id
                fileLeve = entity.FileLeve,
                entity.LawRegulationsName,//法律法规名称
                entity.Symbol,//文号/标准号
                entity.IssueDate,//颁布日期
                mark01 = Convert.ToDateTime(entity.IssueDate).ToString("yyyy-MM-dd"),
                entity.ImplementationDate,//实施日期
                mark02 = Convert.ToDateTime(entity.ImplementationDate).ToString("yyyy-MM-dd"),
                attachmentList, //附件
                entity.IssueSubject,//发布主体
                entity.UpdateTime,//更新时间
                entity.ApplyClause,//适用条款
                entity.DescribeForWork,//对应工作情况描述
                entity.Remarks,
                // DeptName = standardInformationDept.de
                //  departmentId = entity.SysDepartment2.Id,
                entity.FL_StandardInformations,
                entity.Attachment,
                entity.AttachmentName,
                entity.UpLoadDate,
              


                FL_StandardInformationDept1 = standardInformationDept?.Where(a => a.DeptType == "1").ToList(),//归口部门
                gkbmList=_coreDBContext.SysDepartment.Where(x=>x.Id== DepId)?.Select(a =>
                            new
                            {
                                a.Name,  //归口部门
                            }),

                FL_StandardInformationDept2 = standardInformationDept?.Where(a => a.DeptType == "2").ToList(),//实施部门
                FL_StandardInformationDept3 = standardInformationDept?.Where(a => a.DeptType == "3").ToList(),//了解部门

                //zxbmList= standardInformationDept?.Where(a => a.DeptType == "2"|| a.DeptType == "3").ToList(),//实施部门 和 了解部门
                zxbmList,

                standardInformationDept,
                CAM_Compliance,
                CAM_ComplianceStatement,
                CAM_Mark,


            };

            return this.JsonSuccess(retModel);
        }

        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <returns></returns>

        [HttpPost("getFactory")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> getFactory(PagingModel<FL_StandardInformationDto>
               pagingQueryModel)
        {
            var CreateFactory = UserIdentity.Factory;//工厂
            var retModel = new
            {
                CreateFactory
            };

            return this.JsonSuccess(retModel);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("Export")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + nameof(FX_EquipmentFacilitiesController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> Export(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        /// <summary>
        /// 信息补录 ---导出
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("exportMes")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + nameof(FX_EquipmentFacilitiesController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> exportMes(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            var fileName = await _service.ExportMes(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        /// <summary>
        /// 信息清单 ---导出
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("exportMesQD")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + nameof(FX_EquipmentFacilitiesController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> exportMesQD(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            var fileName = await _service.ExportMesQD(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        /// <summary>
        /// 合规性符合性评审 ---导出
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("Comexport")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + nameof(FX_EquipmentFacilitiesController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> Comexport(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            var fileName = await _service.Comexport(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }




        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="data"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("Import")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + "导入")]
        public async Task<IActionResult> ImportAsync([FromForm]String data, [FromForm]List<IFormFile> file)
        {
            var retMeg = await _service.ImportAsync(file[0], data);
            if (!retMeg.Any())
            {
                _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                {
                    FromUser = UserIdentity.Id,
                    ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.AdminHSEDeptName).FirstOrDefault(),
                    //  Content = $"消息模块测试当前222时间:{DateTime.Now}",
                    Content = $"法律法规模块在[" + DateTime.Now + "]完成一次导入,请部门经理验收",
                    SendTime = DateTime.Now,
                    Ttitle = "操作程序-添加申请"
                });

                return this.JsonSuccess();
            }
            else
            {
                return this.JsonFaild(retMeg[0].ToString());
            }
        }


        /// <summary>
        ///  修改
        ///</summary>
        /// <param name="dto">含审批表id，审批结果，审批意见</param>
        /// <returns></returns>
        [HttpPost("Save")]
        //[PermissionFilter(nameof(FX_ProcessMediumRecordController) + "_" + nameof(FX_ProcessMediumRecordController.SaveAsync))]
        public async Task<IActionResult> SaveAsync(FL_StandardInformationDto dto)
        {
            var oldEntity = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "2" ).ToListAsync();
            oldEntity.ForEach(e => _dbContext.FL_StandardInformationDept.Remove(e));

            var newEntity = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept2);
            newEntity.ForEach(e => { e.LawRegulationsName = dto.Id; e.DeptType = "2"; e.DeptTypeText = "实施部门"; e.FitSituation = dto.FL_StandardInformationDept2.FirstOrDefault().FitSituation;
                e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept2.FirstOrDefault().FitSituationOrNotDescribe;
            });
            // 
            await _dbContext.AddRangeAsync(newEntity);
            await _dbContext.SaveChangesAsync();

            var oldEntity3 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "3").ToListAsync();
            oldEntity3.ForEach(e => _dbContext.FL_StandardInformationDept.Remove(e));

            var newEntity3 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept3);
            newEntity3.ForEach(e => { e.LawRegulationsName = dto.Id; e.DeptType = "3"; e.DeptTypeText = "了解部门"; });
            //
            await _dbContext.AddRangeAsync(newEntity3);
            await _dbContext.SaveChangesAsync();


            //dto.FL_StandardInformationDept1.FirstOrDefault().Class = await _dbContext.SysDepartment.Where(x => x.Name == "123").FirstOrDefault().Id;

            var oldEntity1 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "1").ToListAsync();
            oldEntity1.ForEach(e => _dbContext.FL_StandardInformationDept.Remove(e));

            var newEntity1 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept1);
            newEntity1.ForEach(e => { e.LawRegulationsName = dto.Id; e.DeptType = "1"; e.DeptTypeText = "归口部门"; });
            //

            await _dbContext.AddRangeAsync(newEntity1);
            await _dbContext.SaveChangesAsync();

            var entity = await _service.GetAsync(dto.Id);

            dto.UpdateUser = UserIdentity.Id;
            dto.FileLeve = entity.FileLeve;
            dto.IssueSubject = entity.IssueSubject;
            dto.Remarks = entity.Remarks;
            dto.State = entity.State;
            dto.Mark01 = entity.Mark01;
            dto.InputPerson = entity.InputPerson;
            dto.InputTime = entity.InputTime;
            dto.CreateFactory = UserIdentity.Factory;//工厂


            await _service.UpdateAsync(dto, true, new List<string> { "FL_StandardInformations" });
            try
            {
                _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                {
                    FromUser = UserIdentity.Id,
                    ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                    Content = $"法律法规信息补录模块在[" + DateTime.Now + "]完成一次信息补录,请部门经理验收",
                    SendTime = DateTime.Now,
                    Ttitle = "操作程序-添加申请"
                });

                _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                {
                    FromUser = UserIdentity.Id,
                    ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.ZXDeptName).FirstOrDefault(),
                    Content = $"法律法规信息补录模块在[" + DateTime.Now + "]完成一次信息补录,请部门经理验收",
                    SendTime = DateTime.Now,
                    Ttitle = "操作程序-添加申请"
                });

                _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                {
                    FromUser = UserIdentity.Id,
                    ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.LJDeptName).FirstOrDefault(),
                    Content = $"法律法规信息补录模块在[" + DateTime.Now + "]完成一次信息补录,请部门经理验收",
                    SendTime = DateTime.Now,
                    Ttitle = "操作程序-添加申请"
                });
            }
            catch (Exception ex )
            {
                throw ex ;
            }
            return this.JsonSuccess(string.Empty);
        }


        /// <summary>
        /// 审批 
        ///</summary>
        /// <param name="dto">含审批表id，审批结果，审批意见</param>
        /// <returns></returns>
        [HttpPost("SaveSP")]
        //[PermissionFilter(nameof(FX_ProcessMediumRecordController) + "_" + nameof(FX_ProcessMediumRecordController.SaveAsync))]
        public async Task<IActionResult> SaveAsyncSP(FL_StandardInformationDto dto)
        {
         //   var entity = await _service.GetAsync(dto.Id);

            dto.UpdateUser = UserIdentity.Id;
            //dto.FileLeve = entity.FileLeve;
            //dto.IssueSubject = entity.IssueSubject;
            //dto.Remarks = entity.Remarks;
            dto.State = 1.ToString();
            //dto.Mark01 = entity.Mark01;
            //dto.InputPerson = entity.InputPerson;
            //dto.InputTime = entity.InputTime;

            await _service.UpdateAsync(dto, true, new List<string> { "FL_StandardInformations" });
            return this.JsonSuccess(string.Empty);
        }

        /// <summary>
        /// 合规性评价及符合性----评审
        ///</summary>
        /// <param name="dto">含审批表id，审批结果，审批意见</param>
        /// <returns></returns>
        [HttpPost("ComSave")]
        //[PermissionFilter(nameof(FX_ProcessMediumRecordController) + "_" + nameof(FX_ProcessMediumRecordController.SaveAsync))]
        public async Task<IActionResult> ComSave(FL_StandardInformationDto dto)
        {
            var FL_StandardInformationReview = new FL_StandardInformationReview();

            if (dto.CAM_Compliance=="符合")
            {
                FL_StandardInformationReview.FitSituationOrNotDescribe = "";
                FL_StandardInformationReview.FitSituation = dto.CAM_ComplianceStatement;
                FL_StandardInformationReview.CAM_ReviewDate = DateTime.Now;
                FL_StandardInformationReview.CAM_ReviewPerson = UserIdentity.Id;
                if (_coreDBContext.FL_StandardInformationDept.Where(
                    x => x.LawRegulationsName == dto.Id && x.DeptType == "1").ToList().Count > 0)
                {
                    FL_StandardInformationReview.CAM_Dept = _coreDBContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "1").FirstOrDefault().Class;
                }
                FL_StandardInformationReview.Remarks = dto.CAM_Mark;
                FL_StandardInformationReview.StandardInformationID = dto.Id;
                FL_StandardInformationReview.Id = Guid.Empty;
            }
            if (dto.CAM_Compliance == "不符合")
            {

                FL_StandardInformationReview.FitSituationOrNotDescribe = dto.CAM_ComplianceStatement;
                FL_StandardInformationReview.FitSituation ="";
                FL_StandardInformationReview.CAM_ReviewDate = DateTime.Now;
                FL_StandardInformationReview.CAM_ReviewPerson = UserIdentity.Id;
                if (_coreDBContext.FL_StandardInformationDept.Where(
                    x => x.LawRegulationsName == dto.Id && x.DeptType == "1").ToList().Count > 0)
                {
                    FL_StandardInformationReview.CAM_Dept = _coreDBContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "1").FirstOrDefault().Class;
                }
                FL_StandardInformationReview.Remarks = dto.CAM_Mark;
                FL_StandardInformationReview.StandardInformationID = dto.Id;
                FL_StandardInformationReview.Id = Guid.Empty;
               
            }
            FL_StandardInformationReview.CAM_Type = "正常";
            await _dbContext.AddRangeAsync(FL_StandardInformationReview);
            await _dbContext.SaveChangesAsync();
        //    await _serviceReview.InsertAsync()
            return this.JsonSuccess(string.Empty);
        }




        /// <summary>
        /// 法律法规清单添加--插入历史清单 FL_StandardInformationHis
        ///</summary>
        /// <param name="dto">含审批表id，审批结果，审批意见</param>
        /// <returns></returns>
        [HttpPost("SaveHistory")]
        //[PermissionFilter(nameof(FX_ProcessMediumRecordController) + "_" + nameof(FX_ProcessMediumRecordController.SaveAsync))]
        public async Task<IActionResult> SaveHistory(FL_StandardInformationHisDto dto)
        {
            FL_StandardInformationDto FLDto = new FL_StandardInformationDto();
            FLDto.Id = dto.Id;
            FLDto.IsDeleted = false;
            FLDto.CreateUser = dto.CreateUser;
            FLDto.CreateTime = dto.CreateTime;
            FLDto.FileLeve = dto.FileLeve; 
            FLDto.LawRegulationsName = dto.LawRegulationsName;
            FLDto.IssueSubject = dto.IssueSubject;
            FLDto.Symbol = dto.Symbol;
            FLDto.IssueDate = dto.IssueDate;
            FLDto.ImplementationDate = dto.ImplementationDate;
            FLDto.ApplyClause = dto.ApplyClause;
            FLDto.DescribeForWork = dto.DescribeForWork;
            FLDto.Remarks = dto.Remarks ;
            FLDto.State = "0";
            FLDto.UpLoadDate = dto.UpLoadDate;
            FLDto.InputPerson = UserIdentity.Id;
            FLDto.InputTime = System.DateTime.Now;
            FLDto.Attachment = dto.Attachment;
            FLDto.AttachmentName = dto.AttachmentName;
            FLDto.CreateFactory = UserIdentity.Factory;

            if (Equals(dto.Id, Guid.Empty))
            {
                if (FLDto.Attachment != null)
                {
                    FLDto.UpLoadDate = System.DateTime.Now;
                }
                FLDto.Source = "添加";
                await _service.InsertAsync(FLDto);

                var FLEntityDepId = _dbContext.FL_StandardInformation.Select(x =>
               new
               {
                   x.Id,
                   x.InputTime
               }).ToList().OrderBy(s => s.InputTime);
                var LawRegulationsName = FLEntityDepId.Select(x => x.Id).LastOrDefault();


                var FLnewEntity = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept1);
                FLnewEntity.ForEach(e =>
                {
                    e.LawRegulationsName = LawRegulationsName; e.DeptType = "1"; e.DeptTypeText = "归口部门"; e.FitSituation = dto.FL_StandardInformationDept1.FirstOrDefault().FitSituation;
                    e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept1.FirstOrDefault().FitSituationOrNotDescribe;
                });
                //
                await _dbContext.AddRangeAsync(FLnewEntity);
                await _dbContext.SaveChangesAsync();

                var FLnewEntity2 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept2);
                FLnewEntity2.ForEach(e =>
                {
                    e.LawRegulationsName = LawRegulationsName;
                    e.DeptType = "2";
                    e.DeptTypeText = "实施部门";
                    e.FitSituation = dto.FL_StandardInformationDept2.FirstOrDefault().FitSituation;
                    e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept2.FirstOrDefault().FitSituationOrNotDescribe;
                });
                //
                await _dbContext.AddRangeAsync(FLnewEntity2);
                await _dbContext.SaveChangesAsync();

                var FLnewEntity3 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept3);
                FLnewEntity3.ForEach(e =>
                {
                    e.LawRegulationsName = LawRegulationsName;
                    e.DeptType = "3";
                    e.DeptTypeText = "了解部门";
                    e.FitSituation = dto.FL_StandardInformationDept3.FirstOrDefault().FitSituation;
                    e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept3.FirstOrDefault().FitSituationOrNotDescribe;
                });
                //
                await _dbContext.AddRangeAsync(FLnewEntity3);
                await _dbContext.SaveChangesAsync();

                //发送消息
                try
                {
                    _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                    {
                        FromUser = UserIdentity.Id,
                        ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                        Content = $"法律法规信息清单模块在[" + DateTime.Now + "]完成一次信息新增",
                        SendTime = DateTime.Now,
                        Ttitle = "操作程序-添加申请"
                    });

                    _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                    {
                        FromUser = UserIdentity.Id,
                        ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.ZXDeptName).FirstOrDefault(),
                        Content = $"法律法规信息清单模块在[" + DateTime.Now + "]完成一次信息新增",
                        SendTime = DateTime.Now,
                        Ttitle = "操作程序-添加申请"
                    });

                    _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                    {
                        FromUser = UserIdentity.Id,
                        ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.LJDeptName).FirstOrDefault(),
                        Content = $"法律法规信息清单模块在[" + DateTime.Now + "]完成一次信息新增",
                        SendTime = DateTime.Now,
                        Ttitle = "操作程序-添加申请"
                    });
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            else
            {
                if(dto.LawRegulationsName != _dbContext.FL_StandardInformation.Where(x => x.Id == dto.Id).FirstOrDefault().LawRegulationsName)
                {
                    if (_dbContext.FL_StandardInformation.Where(x => x.LawRegulationsName == dto.LawRegulationsName).ToList().Count > 0)
                    {
                        return this.JsonFaild("法律法规名称重复！");
                    }
                }
             


                var FLoldEntity1 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "1").ToListAsync();
                FLoldEntity1.ForEach(e => _dbContext.FL_StandardInformationDept.Remove(e));

                var FLnewEntity1 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept1);
                FLnewEntity1.ForEach(e =>
                {
                    e.LawRegulationsName = dto.Id;
                    e.DeptType = "1";
                    e.DeptTypeText = "归口部门";
                    e.FitSituation = dto.FL_StandardInformationDept1.FirstOrDefault().FitSituation;
                    e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept1.FirstOrDefault().FitSituationOrNotDescribe;
                });
                // 
                await _dbContext.AddRangeAsync(FLnewEntity1);
                await _dbContext.SaveChangesAsync();

                var FLoldEntity2 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "2").ToListAsync();
                FLoldEntity2.ForEach(e => _dbContext.FL_StandardInformationDept.Remove(e));

                var FLnewEntity2 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept2);
                FLnewEntity2.ForEach(e =>
                {
                    e.LawRegulationsName = dto.Id;
                    e.DeptType = "2";
                    e.DeptTypeText = "实施部门";
                    e.FitSituation = dto.FL_StandardInformationDept2.FirstOrDefault().FitSituation;
                    e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept2.FirstOrDefault().FitSituationOrNotDescribe;
                });
                //
                await _dbContext.AddRangeAsync(FLnewEntity2);
                await _dbContext.SaveChangesAsync();

                var FLoldEntity3 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == dto.Id && x.DeptType == "3").ToListAsync();
                FLoldEntity3.ForEach(e => _dbContext.FL_StandardInformationDept.Remove(e));

                var FLnewEntity3 = Mapper.Map<List<FL_StandardInformationDept>>(dto.FL_StandardInformationDept3);
                FLnewEntity3.ForEach(e =>
                {
                    e.LawRegulationsName = dto.Id;
                    e.DeptType = "3";
                    e.DeptTypeText = "了解部门";
                    e.FitSituation = dto.FL_StandardInformationDept3.FirstOrDefault().FitSituation;
                    e.FitSituationOrNotDescribe = dto.FL_StandardInformationDept3.FirstOrDefault().FitSituationOrNotDescribe;
                });
                //
                await _dbContext.AddRangeAsync(FLnewEntity3);
                await _dbContext.SaveChangesAsync();


                //判断上传文件时间是否更新
                var FileEntity = await _dbContext.FL_StandardInformation.Where(x => x.Id == dto.Id).FirstOrDefaultAsync();
                if (FLDto.Attachment == null)
                {
                    FLDto.UpLoadDate = null;
                }
                if (FLDto.Attachment != FileEntity.Attachment && FLDto.Attachment != null)
                {
                    FLDto.UpLoadDate = DateTime.Now;
                    dto.UpLoadDate = DateTime.Now;
                }
                FLDto.Source = "修改";
                FLDto.CreateFactory = UserIdentity.Factory;

                await _service.UpdateAsync(FLDto, true, new List<string> { "FL_StandardInformations" });

                //发送消息
                try
                {
                    _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                    {
                        FromUser = UserIdentity.Id,
                        ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                        Content = $"法律法规信息清单模块在[" + DateTime.Now + "]完成一次信息更新",
                        SendTime = DateTime.Now,
                        Ttitle = "操作程序-更新申请"
                    });

                    _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                    {
                        FromUser = UserIdentity.Id,
                        ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.ZXDeptName).FirstOrDefault(),
                        Content = $"法律法规信息清单模块在[" + DateTime.Now + "]完成一次信息更新",
                        SendTime = DateTime.Now,
                        Ttitle = "操作程序-更新申请"
                    });

                    _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                    {
                        FromUser = UserIdentity.Id,
                        ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.LJDeptName).FirstOrDefault(),
                        Content = $"法律法规信息清单模块在[" + DateTime.Now + "]完成一次信息更新",
                        SendTime = DateTime.Now,
                        Ttitle = "操作程序-更新申请"
                    });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


            //插入法律法规历史表单
            dto.InputPerson = UserIdentity.Id;
            dto.InputTime = System.DateTime.Now;
            dto.CreateTime = System.DateTime.Now;
            dto.State = "0";
            dto.Source = FLDto.Source;
            dto.CreateFactory = UserIdentity.Factory;

            if (dto.Attachment == null)
            {
                dto.UpLoadDate = null;
            }
            if (Equals(dto.Id, Guid.Empty) && dto.Attachment != null)
            {
                dto.UpLoadDate = DateTime.Now;
            }


            dto.Id = Guid.NewGuid();
           
            await _Hisservice.InsertAsync(dto);

            var EntityDepId = _dbContext.FL_StandardInformationHis.Select(x =>
             new
             {
                 x.Id,
                 x.InputTime
             }).ToList().OrderBy(s => s.InputTime);
            var StandardInformationHisId = EntityDepId.Select(x => x.Id).LastOrDefault();

            var ClearEntity1 = await _dbContext.FL_StandardInformationHisDept.Where(x => x.StandardInformationHisId == StandardInformationHisId).ToListAsync();
            ClearEntity1.ForEach(e => _dbContext.FL_StandardInformationHisDept.Remove(e));

            var newEntity = Mapper.Map<List<FL_StandardInformationHisDept>>(dto.FL_StandardInformationDept1);
            newEntity.ForEach(e => { e.StandardInformationHisId = StandardInformationHisId; e.DeptType = "1"; });
            await _dbContext.AddRangeAsync(newEntity);
            await _dbContext.SaveChangesAsync();



            var newEntity2 = Mapper.Map<List<FL_StandardInformationHisDept>>(dto.FL_StandardInformationDept2);
            newEntity2.ForEach(e => { e.StandardInformationHisId = StandardInformationHisId; e.DeptType = "2"; });
            await _dbContext.AddRangeAsync(newEntity2);
            await _dbContext.SaveChangesAsync();



            var newEntity3 = Mapper.Map<List<FL_StandardInformationHisDept>>(dto.FL_StandardInformationDept3);
            newEntity3.ForEach(e => { e.StandardInformationHisId = StandardInformationHisId; e.DeptType = "3"; });
            await _dbContext.AddRangeAsync(newEntity3);
            await _dbContext.SaveChangesAsync();

            return this.JsonSuccess(string.Empty);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        //[PermissionFilter(nameof(RoleController) + "_" + nameof(RoleController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            List<Guid> newList = new List<Guid>();
            for (int i = 0; i < ids.Count(); i++)
            {
                if (_coreDBContext.FL_StandardInformation.Where(x => x.Id == ids[i]).Where(b => b.IsDeleted == false).ToList().Count > 0)
                {
                    newList.Add(ids[i]);
                }
            }
       //     await _service.DeleteBatchAsync(newList.ToArray());


            for (int i = 0; i < newList.Count; i++)
            {
                var entity = await _service.GetAsync(newList[i]);
                if(entity != null)
                {
                    FL_StandardInformationDto Dto = new FL_StandardInformationDto();
                    Dto.Id = entity.Id;
                    Dto.IsDeleted = true;
                    Dto.CreateUser = entity.CreateUser;
                    Dto.CreateTime = entity.CreateTime;
                    Dto.FileLeve = entity.FileLeve;
                    Dto.LawRegulationsName = entity.LawRegulationsName;
                    Dto.IssueSubject = entity.IssueSubject;
                    Dto.Symbol = entity.Symbol;
                    Dto.IssueDate = entity.IssueDate;
                    Dto.ImplementationDate = entity.ImplementationDate;
                    Dto.ApplyClause = entity.ApplyClause;
                    Dto.DescribeForWork = entity.DescribeForWork;
                    Dto.Remarks = entity.Remarks;
                    Dto.State = "0";
                    Dto.UpLoadDate = entity.UpLoadDate;
                    Dto.InputPerson = UserIdentity.Id;
                    Dto.InputTime = System.DateTime.Now;
                    Dto.Attachment = entity.Attachment;
                    Dto.AttachmentName = entity.AttachmentName;
                    Dto.UpdateTime = entity.UpdateTime;
                    Dto.Source = "删除";
                    Dto.CreateFactory = UserIdentity.Factory;
                    await _service.UpdateAsync(Dto, true, new List<string> { "FL_StandardInformations", "FL_StandardInformationReview" });
                }


                //插入法律法规历史表单
                FL_StandardInformationHisDto HisDto = new FL_StandardInformationHisDto();
             
                HisDto.StandardInformationId = entity.Id;
                HisDto.CreateUser = entity.CreateUser;
                HisDto.CreateTime = entity.CreateTime;
                HisDto.FileLeve = entity.FileLeve;
                HisDto.LawRegulationsName = entity.LawRegulationsName;
                HisDto.IssueSubject = entity.IssueSubject;
                HisDto.Symbol = entity.Symbol;
                HisDto.IssueDate = entity.IssueDate;
                HisDto.ImplementationDate = entity.ImplementationDate;
                HisDto.ApplyClause = entity.ApplyClause;
                HisDto.DescribeForWork = entity.DescribeForWork;
                HisDto.Remarks = entity.Remarks;
                HisDto.State = "0";
                HisDto.UpLoadDate = entity.UpLoadDate;
                HisDto.InputPerson = UserIdentity.Id;
                HisDto.InputTime = System.DateTime.Now;
                HisDto.Attachment = entity.Attachment;
                HisDto.AttachmentName = entity.AttachmentName;
                HisDto.InputPerson = UserIdentity.Id;
                HisDto.InputTime = System.DateTime.Now;
                HisDto.State = "0";
                HisDto.UpdateTime = entity.UpdateTime;
                HisDto.Source = "删除";
                HisDto.CreateFactory = UserIdentity.Factory;
                HisDto.Id = Guid.Empty;

                await _Hisservice.InsertAsync(HisDto);

                var EntityDepId = _dbContext.FL_StandardInformationHis.Select(x =>
                 new
                 {
                     x.Id,
                     x.InputTime
                 }).ToList().OrderBy(s => s.InputTime);
                var StandardInformationHisId = EntityDepId.Select(x => x.Id).LastOrDefault();

                var ClearEntity1 = await _dbContext.FL_StandardInformationHisDept.Where(x => x.StandardInformationHisId == StandardInformationHisId).ToListAsync();
                ClearEntity1.ForEach(e => _dbContext.FL_StandardInformationHisDept.Remove(e));

                var DepList1= await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == entity.Id && x.DeptType=="1").ToListAsync();


                var newEntity = Mapper.Map<List<FL_StandardInformationHisDept>>(DepList1);
                newEntity.ForEach(e => { e.StandardInformationHisId = StandardInformationHisId; e.DeptType = "1"; });
                await _dbContext.AddRangeAsync(newEntity);
                await _dbContext.SaveChangesAsync();

                var DepList2 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == entity.Id && x.DeptType == "2").ToListAsync();

                var newEntity2 = Mapper.Map<List<FL_StandardInformationHisDept>>(DepList2);
                newEntity2.ForEach(e => { e.StandardInformationHisId = StandardInformationHisId; e.DeptType = "2"; });
                await _dbContext.AddRangeAsync(newEntity2);
                await _dbContext.SaveChangesAsync();


                var DepList3 = await _dbContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == entity.Id && x.DeptType == "3").ToListAsync();

                var newEntity3 = Mapper.Map<List<FL_StandardInformationHisDept>>(DepList3);
                newEntity3.ForEach(e => { e.StandardInformationHisId = StandardInformationHisId; e.DeptType = "3"; });
                await _dbContext.AddRangeAsync(newEntity3);
                await _dbContext.SaveChangesAsync();
            }

            return this.JsonSuccess(string.Empty);
        }
    }
}
